/*
 * Date         Author          Note
 * 2015.04.15   wzwz            起底板
 * 2015.05.25   wzwz            剪夹联动
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mh/common.h>
#include <mh/sram.h>
#include <mh/mac.h>
#include "knit.h"
#include <mh/mh_file.h>
#include "decript.h"
#include "prefixknit.h"
#include "backpiece.h"
#include "board.h"

/*
 *
 */
int GetBoardFeedback(MH_KNIT *p, PBOARD_FEEDBACK pfeedback)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            int count = 0;
            while(count < 10)
            {
                ret = SramGetBoardFeedback(p->pmac, pfeedback);
                if(!ret && pfeedback->can_error)
                {
                    ++count;
                    continue;
                }
                break;
            }
            if(!ret)
            {
                ret = SramGetBoardCoder(p->pmac, &pfeedback->coder);
            }
        }
    }
    return ret;
}
/*
 *
 */
enum {
    BOARD_4GRAB = 0,
    BOARD_4DROP,
    BOARD_OFF,
    BOARD_SAFE,
    BOARD_RESET,
    BOARD_MANUAL_UP,
    BOARD_MANUAL_DOWN,
    BOARD_RELAX,
};
int BoardAct(MH_KNIT *p, int act)
{
    int ret = 0;
    uchar speed = 50;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            int item = -1000;
            enum
            {
                SRAM_STEP_DOWN = -3,
                SRAM_BOARD_RELAX = -2,
                SRAM_BOARD_DOWN = -1,
                SRAM_BOARD_STOP = 0,
                SRAM_BOARD_UP = 1,
                SRAM_BOARD_SAFE = 2,
                SRAM_STEP_UP = 3,
            };
            switch (act)
            {
            case BOARD_4GRAB:
                if(!p->board_state.board_high)
                {
                    item = SRAM_BOARD_UP;
                }
                break;
            case BOARD_4DROP:
                break;
            case BOARD_OFF:
                if(!p->board_state.board_zero)
                {
                    item = SRAM_BOARD_DOWN;
                }
                break;
            case BOARD_SAFE:
                if(!p->board_state.board_safe)
                {
                    item = SRAM_BOARD_SAFE;
                }
                break;
            case BOARD_RESET:
                if(!p->board_state.board_zero)
                {
                    item = SRAM_BOARD_DOWN;
                }
                break;
            case BOARD_MANUAL_UP:
                if(!p->board_state.board_high)
                {
                    item = SRAM_STEP_UP;
                }
                break;
            case BOARD_MANUAL_DOWN:
                if(!p->board_state.board_zero)
                {
                    item = SRAM_STEP_DOWN;
                }
                break;
            case BOARD_RELAX:
                {
                    item = SRAM_BOARD_RELAX;
                }
                break;
            }
            if(item > -1000)
            {
                ret = SramBoardAct(p->pmac, item, speed);
            }
        }
    }
    return ret;
}
/*
 *
 */
int HookOpen(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            if(!p->board_state.hook_open)
            {
                ret = SramHook(p->pmac, TRUE);
            }
        }
    }
    return ret;
}
/*
 *
 */
int HookClose(MH_KNIT *p)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            if(!p->board_state.hook_close)
            {
                ret = SramHook(p->pmac, FALSE);
            }
        }
    }
    return ret;
}
/*
 *
 */
int ClipOn(MH_KNIT *p, int bits)
{
    int ret = 0;
    int i = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            for(i = 0; i < 4; ++i)
            {
                if(bits & (1 << i))
                {
                    if(!p->board_state.clip_off[i])
                    {
                        bits ^= 1 << i;
                    }
                }
            }
            if(bits)
            {
                ret = SramClipOn(p->pmac, bits);
            }
        }
        if(!ret)
        {
            for(i = 0; i < 4; ++i)
            {
                if(bits & (1 << i))
                {
                    p->board_state.clip_off[i] = FALSE;
                }
            }
        }
    }
    return ret;
}
/*
 *
 */
int ClipOff(MH_KNIT *p, int bits)
{
    int ret = 0;
    int i = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramClipOff(p->pmac, bits);
        }
        if(!ret)
        {
            for(i = 0; i < 4; ++i)
            {
                if(bits & (1 << i))
                {
                    p->board_state.clip_off[i] = TRUE;
                }
            }
        }
    }
    return ret;
}
/*
 *
 */
int Scissor(MH_KNIT *p, int bits)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramScissor(p->pmac, bits);
        }
    }
    return ret;
}
/*
 *
 */
int GetLeftClipEnable(MH_KNIT *p)
{
    return p->mac.clip_enable;
}
/*
 *
 */
void SetLeftClipEnable(MH_KNIT *p, int enable)
{
    p->mac.clip_enable = enable;
}
/*
 *
 */
int GetRightClipEnable(MH_KNIT *p)
{
    return p->mac.right_clip_enable;
}
/*
 *
 */
void SetRightClipEnable(MH_KNIT *p, int enable)
{
    p->mac.right_clip_enable = enable;
}
/*
 *
 */
ushort GetYarnLeftOutStop(MH_KNIT *p)
{
    return p->yarn_out.yarn_left_out * p->inch_type / MM_PER_INCH;
}
/*
 *
 */
ushort GetYarnLeftOut(MH_KNIT *p)
{
    return p->yarn_out.yarn_left_out;
}
/*
 * needle
 */
ushort GetYarnRightOutStop(MH_KNIT *p)
{
    return p->yarn_out.yarn_right_out * p->inch_type / MM_PER_INCH;
}
/*
 * mm
 */
ushort GetYarnRightOut(MH_KNIT *p)
{
    return p->yarn_out.yarn_right_out;
}
/*
 * 最高位(勾线位)
 */
int GetBoardParamHighPos(MH_KNIT *p)
{
    return p->board_param.high_pos;
}
/*
 * 安全位
 */
int GetBoardParamSafePos(MH_KNIT *p)
{
    return p->board_param.safe_pos;
}
/*
 * 罗拉合上位置
 */
int GetBoardParamRollClosePos(MH_KNIT *p)
{
    return p->board_param.pos_for_roll_close;
}
/*
 * 脱圈位(高罗拉)
 */
int GetBoardParamRollHookOpenPos(MH_KNIT *p)
{
    return p->board_param.hook_open_pos_for_roll;
}
/*
 * 脱圈位滞后
 */
int GetBoardParamHookOpenDelay(MH_KNIT *p)
{
    return p->board_param.delay_for_hook_open;
}
/*
 * 起底板上升初始速度
 */
int GetBoardParamUpStartSpeed(MH_KNIT *p)
{
    return p->board_param.speed_for_up_start;
}
/*
 * 起底板上升加速速度
 */
int GetBoardParamUpQuickSpeed(MH_KNIT *p)
{
    return p->board_param.speed_for_up_quick;
}
/*
 * 起底板上升减速速度
 */
int GetBoardParamUpSlowSpeed(MH_KNIT *p)
{
    return p->board_param.speed_for_up_slow;
}
/*
 * 降到安全位速度
 */
int GetBoardParamToSafeSpeed(MH_KNIT *p)
{
    return p->board_param.speed_for_drop_2safe_pos;
}
/*
 * 起底板复位速度
 */
int GetBoardParamResetSpeed(MH_KNIT *p)
{
    return p->board_param.reset_speed;
}
/*
 * 起底板下降减速位置
 */
int GetBoardParamDownSlowPos(MH_KNIT *p)
{
    return p->board_param.pos_for_down_slow;
}
/*
 * 起底板下降减速速度
 */
int GetBoardParamDownSlowSpeed(MH_KNIT *p)
{
    return p->board_param.speed_for_down_slow;
}
/*
 * 落布时罗拉速度
 */
int GetBoardParamDropRollSpeed(MH_KNIT *p)
{
    return p->board_param.speed_for_roll_when_drop;
}
/*
 * 落布等待时间
 */
int GetBoardParamDropWaitTime(MH_KNIT *p)
{
    return p->board_param.wait_for_drop;
}
/*
 *
 */
short GetBoardParamType(MH_KNIT *p)
{
    return p->board_param.board_type;
}
/*
 * 起底板上升等待位置
 */
short GetBoardParamUpWaitPos(MH_KNIT *p)
{
    return p->board_param.pos_for_up_wait;
}
/*
 * 起底板上升加速位置
 */
short GetBoardParamUpQuickPos(MH_KNIT *p)
{
    return p->board_param.pos_for_up_quick;
}
/*
 * 起底板上升减速位置
 */
short GetBoardParamUpSlowPos(MH_KNIT *p)
{
    return p->board_param.pos_for_up_slow;
}
/*
 * 起底板下降脱圈速度
 */
int GetBoardParamDownHookOpenSpeed(MH_KNIT *p)
{
    return p->board_param.speed_down_after_hook_open;
}
/*
 * 平衡值
 */
uchar GetBoardParamBalance(MH_KNIT *p)
{
    return p->board_param.balance;
}
/*
 * 剪刀次数
 */
uchar GetBoardParamScissorTimes(MH_KNIT *p)
{
    return p->board_param.scissor_times;
}
/*
 * 脱圈位2(主拉布)
 */
int GetBoardParamRabHookOpenPos(MH_KNIT *p)
{
    return p->board_param.hook_open_pos_for_rab;
}
/*
 * 主拉布合上速度
 */
short GetRabCloseSpeed(MH_KNIT *p)
{
    return p->board_param.rab_close_speed;
}
/*
 * 主拉布打开速度
 */
short GetRabOpenSpeed(MH_KNIT *p)
{
    return p->board_param.rab_open_speed;
}
/*
 * 主拉布合上位置
 */
int GetBoardParamRabClosePos(MH_KNIT *p)
{
    return p->board_param.pos_for_rab_close;
}
/*
 *
 */
uchar GetBoardParamGauze(MH_KNIT *p)
{
    return p->board_param.gauze;
}
/*
 *
 */
uchar GetBoardParamKnitMode(MH_KNIT *p)
{
    return p->board_param.start_knit_mode;
}
/*
 *
 */
uchar GetBoardParamPull(MH_KNIT *p)
{
    return p->board_param.pull;
}
/*
 *
 */
uchar GetBoardParamCSSyn(MH_KNIT *p)
{
    return p->board_param.cs_syn;
}
/*
 *
 */
void SetYarnLeftOut(MH_KNIT *p, ushort n)
{
    p->yarn_out.yarn_left_out = n;
}
/*
 *
 */
void SetYarnRightOut(MH_KNIT *p, ushort n)
{
    p->yarn_out.yarn_right_out = n;
}
/*
 * 最高位(勾线位)
 */
void SetBoardParamHighPos(MH_KNIT *p, int pulse)
{
    p->board_param.high_pos = pulse;
}
/*
 * 安全位
 */
void SetBoardParamSafePos(MH_KNIT *p, int pulse)
{
    p->board_param.safe_pos = pulse;
}
/*
 * 罗拉合上位置
 */
void SetBoardParamRollClosePos(MH_KNIT *p, int pulse)
{
    p->board_param.pos_for_roll_close = pulse;
}
/*
 * 脱圈位(高罗拉)
 */
void SetBoardParamRollHookOpenPos(MH_KNIT *p, int pulse)
{
    p->board_param.hook_open_pos_for_roll = pulse;
}
/*
 * 脱圈位滞后
 */
void SetBoardParamHookOpenDelay(MH_KNIT *p, int pulse)
{
    p->board_param.delay_for_hook_open = pulse;
}
/*
 * 起底板上升初始速度
 */
void SetBoardParamUpStartSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_for_up_start = speed;
}
/*
 * 起底板上升加速速度
 */
void SetBoardParamUpQuickSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_for_up_quick = speed;
}
/*
 * 起底板上升减速速度
 */
void SetBoardParamUpSlowSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_for_up_slow = speed;
}
/*
 * 降到安全位速度
 */
void SetBoardParamToSafeSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_for_drop_2safe_pos = speed;
}
/*
 * 起底板复位速度
 */
void SetBoardParamResetSpeed(MH_KNIT *p, int speed)
{
    p->board_param.reset_speed = speed;
}
/*
 * 起底板下降减速位置
 */
void SetBoardParamDownSlowPos(MH_KNIT *p, int pulse)
{
    p->board_param.pos_for_down_slow = pulse;
}
/*
 * 起底板下降减速速度
 */
void SetBoardParamDownSlowSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_for_down_slow = speed;
}
/*
 * 落布时罗拉速度
 */
void SetBoardParamDropRollSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_for_roll_when_drop = speed;
}
/*
 * 落布等待时间
 */
void SetBoardParamDropWaitTime(MH_KNIT *p, int tm)
{
    p->board_param.wait_for_drop = tm;
}
/*
 *
 */
void SetBoardParamType(MH_KNIT *p, short type)
{
    p->board_param.board_type = type;
}
/*
 * 起底板上升等待位置
 */
void SetBoardParamUpWaitPos(MH_KNIT *p, short pulse)
{
    p->board_param.pos_for_up_wait = pulse;
}
/*
 * 起底板上升加速位置
 */
void SetBoardParamUpQuickPos(MH_KNIT *p, short pulse)
{
    p->board_param.pos_for_up_quick = pulse;
}
/*
 * 起底板上升减速位置
 */
void SetBoardParamUpSlowPos(MH_KNIT *p, short pulse)
{
    p->board_param.pos_for_up_slow = pulse;
}
/*
 * 起底板下降脱圈速度
 */
void SetBoardParamDownHookOpenSpeed(MH_KNIT *p, int speed)
{
    p->board_param.speed_down_after_hook_open = speed;
}
/*
 * 平衡值
 */
void SetBoardParamBalance(MH_KNIT *p, uchar balance)
{
    p->board_param.balance = balance;
}
/*
 * 剪刀次数
 */
void SetBoardParamScissorTimes(MH_KNIT *p, uchar times)
{
    p->board_param.scissor_times = times;
}
/*
 * 脱圈位2(主拉布)
 */
void SetBoardParamRabHookOpenPos(MH_KNIT *p, int pulse)
{
    p->board_param.hook_open_pos_for_rab = pulse;
}
/*
 * 主拉布合上速度
 */
void SetRabCloseSpeed(MH_KNIT *p, short speed)
{
    p->board_param.rab_close_speed = speed;
}
/*
 * 主拉布打开速度
 */
void SetRabOpenSpeed(MH_KNIT *p, short speed)
{
    p->board_param.rab_open_speed = speed;
}
/*
 * 主拉布合上位置
 */
void SetBoardParamRabClosePos(MH_KNIT *p, int pulse)
{
    p->board_param.pos_for_rab_close = pulse;
}
/*
 *
 */
void SetBoardParamGauze(MH_KNIT *p, uchar gauze)
{
    p->board_param.gauze = gauze;
}
/*
 *
 */
void SetBoardParamKnitMode(MH_KNIT *p, uchar mode)
{
    p->board_param.start_knit_mode = mode;
}
/*
 *
 */
void SetBoardParamPull(MH_KNIT *p, uchar pull)
{
    p->board_param.pull = pull;
}
/*
 *
 */
void SetBoardParamCSSyn(MH_KNIT *p, uchar syn)
{
    p->board_param.cs_syn = syn;
}
